<?php 
	namespace Helpers\Session;
	function session_init($app){
		$app['session']->invalidate();
		$app['session']->set('uid', 0);
		$app['session']->set('role', 6);

		$user_var['role'] = 6;
		$user_var['display_name'] = '';		
		$app['session']->set('user_var',$user_var);

		$app['session']->set('REMOTE_ADDR', $_SERVER['REMOTE_ADDR']);
		return "";
	}

	function session_restart($app, $sid_old){
		echo "uid before destroy ".$_SESSION['uid']."<br>";
		$_SESSION['uid'] = -1;
		$res = session_init($app);
		echo "uid after destroy ".$_SESSION['uid']."<br>";
		//$app['db']->delete('session', array( "session_id" => $sid_old ) );
	}

	function session_destroy($app){
		$sid = $app['session']->getId();
		$app['db']->delete(
				'session',
				array( 'session_id' => $sid)
			);
	}

	function session_update($app){
		$sid = $app['session']->getId();
		$sql_select = "SELECT count(*) count FROM session WHERE session_id = ? ";
		$res = $app['db']->fetchAssoc( $sql_select, array( (string) $sid ));
		if( $res['count'] > 0 ){
			$sql = "UPDATE session SET session_time = now() WHERE session_id = ? ";
			$app['db']->executeQuery($sql, array( (string) $sid) );
		}else{
			$sql = "INSERT INTO session (session_id, session_time) VALUES( ? , now() )";
			$app['db']->executeQuery($sql, array( (string) $sid) );
		}
	}


	

?>




